//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.ruoyi.framework.web.codetable;

import com.hyjx.framework.common.tree.TreeObject;
import com.hyjx.framework.common.tree.TreeUtil;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class SelectServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    public SelectServlet() {
    }

    public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String codetable = req.getParameter("codetable");
        String parentcode = req.getParameter("parentcode");
        String showtype = req.getParameter("showtype");
        if (showtype == null) {
            this.deal(codetable, parentcode, resp);
        } else {
            this.dealComboTree(codetable, parentcode, resp);
        }

    }

    public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }

    private void dealComboTree(String codetable, String parentcode, HttpServletResponse resp) throws ServletException, IOException {
        List list;
        if (parentcode != null && !parentcode.equals("")) {
            list = CodeTableManager.getSubCodeList(codetable, parentcode);
        } else {
            list = CodeTableManager.getTableList(codetable);
        }

        List<TreeObject> toList = new ArrayList();
        Iterator var7 = list.iterator();

        while(var7.hasNext()) {
            CodeBean cb = (CodeBean)var7.next();
            TreeObject to = new TreeObject();
            to.setId(cb.getCode());
            to.setParent_id(cb.getParentCode());
            to.setText(cb.getName());
            toList.add(to);
        }

        String sb = TreeUtil.getEasyuiJsonStr(toList, "0", (String)null);
        resp.setContentType("text/plain;charset=UTF-8");
        PrintWriter pw = resp.getWriter();
        pw.write(sb);
        pw.flush();
    }

    private void deal(String codetable, String parentcode, HttpServletResponse resp) throws ServletException, IOException {
        List list;
        if (parentcode != null && !parentcode.equals("")) {
            list = CodeTableManager.getSubCodeList(codetable, parentcode);
        } else {
            list = CodeTableManager.getTableList(codetable);
        }

        StringBuffer sb = new StringBuffer();
        sb.append("{\"result\":[");
        int i = 1;
        if (list != null) {
            for(Iterator var8 = list.iterator(); var8.hasNext(); ++i) {
                CodeBean bean = (CodeBean)var8.next();
                sb.append("{");
                sb.append("\"code\":\"" + bean.getCode() + "\",");
                sb.append("\"name\":\"" + bean.getName() + "\"");
                sb.append("}");
                if (i != list.size()) {
                    sb.append(",");
                }
            }
        }

        sb.append("]}");
        resp.setContentType("text/plain;charset=UTF-8");
        PrintWriter pw = resp.getWriter();
        pw.write(sb.toString());
        pw.flush();
    }
}
